<?php
namespace Modules\UI\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Box\Entities\Box;
use \Modules\Product\Entities\Product;
use \Modules\Activity\Entities\Activity;

use Validator;
use ThrowException;
use DB;
use Setting;

class SearchController extends Controller
{
    public function store(Request $req)
    {
        $rule = [
        ];
        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $options = $req->all();

        $options['per_page'] = 10;
        $options['scopes'] = ['onStock'];

        $boxList = [];
        $productList  = [];
        $activityList = [];

        $type = $req->input('item_type');

        $options['with'] = ['image_tag'];

        if ($type == 'all' || $type == 'box') {
            $boxList = Box::getList($options)->getArray([
                'id',
                'uuid',
                'title',
                'money_price',
                'score_price',
                'discount_money_price',
                'discount_score_price',
                'tags',
                'thumb',
                'stock',
                'is_presell',
                'image_tag' => [
                    // 'title',
                    'image',
                    'location'
                ],
            ]);

            foreach ($boxList as $key => $item) {
                $boxList[$key]['item_type'] = 'box';
            }

        }

        if ($type == 'all' || $type == 'product') {
            $productList = Product::getList($options)->getArray([
                'id',
                'uuid',
                'title',
                'money_price',
                'score_price',
                'discount_money_price',
                'discount_score_price',
                'tags',
                'thumb',
                'type',
                'stock',
                'is_presell',
                'image_tag' => [
                    // 'title',
                    'image',
                    'location'
                ],
            ]);

            foreach ($productList as $key => $item) {
                $productList[$key]['item_type'] = 'product';
            }
        }

        // if ($type == 'all' || $type == 'activity') {
        //     $options['scopes']['status'] = 'unexpired';

        //     unset($options['with']);

        //     $activityList = Activity::getList($options)->getArray([
        //         'id',
        //         'uuid',
        //         'title',
        //         'thumb',
        //         'money_price',
        //         'score_price',
        //         'discount_money_price',
        //         'discount_score_price',
        //         'tags',
        //         'type',
        //         'stock',
        //         'is_presell',
        //         'image_tag' => [
        //             // 'title',
        //             'image',
        //             'location'
        //         ],
        //     ]);

        //     foreach ($activityList as $key => $item) {
        //         $activityList[$key]['item_type'] = 'activity';
        //     }
        // }


        $res = array_merge($boxList, $productList,  $activityList);


        return [
            'list' => $res
        ];
    }
}
